package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.page(setmealPageQueryDTO); // page类型设置为VO，接收联合查询查到的分类名称
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void insert(SetmealDTO setmealDTO) {
        // 插入没有自动填充,已解决，虽然设置了公共字段自动填充，只是填在了实体上，sql还是要手动插
        // 插入 setmeal, 传入一个setmeal对象
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setStatus(0);
        setmealMapper.insert(setmeal);
        // 拿到插入sql生成的主键值,需要在xml做一些配置才能拿到
        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            // 给setmeal_dish赋上对应的setmealId值
            setmealDish.setSetmealId(setmealId);
            // 插入 setmeal_dish
            setmealDishMapper.insert(setmealDish);
        });
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        // 看 ids里有没有处于启售的套餐，有的话不能删，返回提示
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        ids.forEach(id -> {
            // 删除 对应id的 setmeal数据
            setmealMapper.deleteById(id);
            // 删除 setmeal_id 对应的 setmeal_dish数据
            setmealDishMapper.deleteBySetmealId(id);
        });


    }

    /**
     * 套餐起售、停售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        if (Objects.equals(status, StatusConstant.ENABLE)) {
            // 要变启售需要检验套餐包含的菜品全部启售
            // 通过当前setmealId从setmeal_dish取到所有属于该套餐的dish_id集合
            List<SetmealDish> setmealDishes = setmealDishMapper.queryById(id);
            // 遍历这个菜品集合去dish里面查status，找到未启售的返回提示信息
            setmealDishes.forEach(setmealDish -> {
                Dish dish = dishMapper.getByid(setmealDish.getDishId());
                if (Objects.equals(dish.getStatus(), StatusConstant.DISABLE)) {
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        setmealMapper.startOrStop(status, id);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO queryById(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        List<SetmealDish> setmealDishes = setmealDishMapper.queryById(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        // 修改 setmeal
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);
        // 拿setmealId
        Long setmealId = setmeal.getId();
        // 删除所有菜品
        setmealDishMapper.deleteBySetmealId(setmealId);
        // 添加传进来的菜品
        setmealDTO.getSetmealDishes().forEach(setmealDish -> {
            setmealDishMapper.insert(setmealDish);
        });
    }

    /**
     * 根据分类id查询套餐
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> queryByCategoryId(Long categoryId) {
        return setmealMapper.queryByCategoryId(categoryId);
    }

    /**
     * 根据套餐id查询包含的菜品
     * @param setmealId
     * @return
     */
    @Override
    public List<DishItemVO> queryDishBySetmealId(Long setmealId) {
        return setmealMapper.queryDishBySetmealId(setmealId);
    }

    // todo 已经删除的套餐对应的关联菜品没删除
}
